<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Electron XLSX测试</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        button { padding: 15px 30px; margin: 10px; font-size: 16px; background: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer; }
        button:hover { background: #218838; }
        .result { margin: 20px 0; padding: 15px; border: 1px solid #ccc; border-radius: 5px; }
        .success { background-color: #d4edda; border-color: #c3e6cb; }
        .error { background-color: #f8d7da; border-color: #f5c6cb; }
        .info { background-color: #d1ecf1; border-color: #bee5eb; }
    </style>
</head>
<body>
    <h1>Electron环境XLSX测试</h1>
    
    <button onclick="testRequire()">测试require加载</button>
    <button onclick="testExport()">测试Excel导出</button>
    
    <div id="result" class="result info">点击按钮开始测试...</div>

    <script>
        function log(message, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.className = `result ${type}`;
            resultDiv.innerHTML = message;
            console.log(message);
        }

        function testRequire() {
            try {
                // 测试require方式加载
                const XLSX = require('xlsx');
                log(`✅ require加载成功！<br>版本: ${XLSX.version}<br>可用方法: ${Object.keys(XLSX.utils).length}个`, 'success');
            } catch (error) {
                log(`❌ require加载失败: ${error.message}`, 'error');
                console.error('Require error:', error);
            }
        }

        function testExport() {
            try {
                log('正在测试Excel导出...', 'info');
                
                // 使用require加载XLSX
                const XLSX = require('xlsx');
                
                // 创建测试数据，包含长数字ID来测试字符串格式
                const data = [
                    ['产品名称', '价格', '库存', 'payeeId'],
                    ['苹果', '5.5', '100', '1234567890123456789'],
                    ['香蕉', '3.2', '150', '9876543210987654321'],
                    ['橙子', '4.8', '80', '5555666677778888999']
                ];

                // 创建工作簿
                const workbook = XLSX.utils.book_new();
                const worksheet = XLSX.utils.aoa_to_sheet(data);
                
                // 设置所有单元格为文本格式，防止数字被自动转换
                const range = XLSX.utils.decode_range(worksheet['!ref']);
                for (let row = range.s.r; row <= range.e.r; row++) {
                    for (let col = range.s.c; col <= range.e.c; col++) {
                        const cellAddress = XLSX.utils.encode_cell({ r: row, c: col });
                        if (worksheet[cellAddress]) {
                            // 设置单元格类型为字符串
                            worksheet[cellAddress].t = 's';
                            // 确保值是字符串
                            if (typeof worksheet[cellAddress].v !== 'string') {
                                worksheet[cellAddress].v = String(worksheet[cellAddress].v);
                            }
                        }
                    }
                }
                
                // 设置列宽
                worksheet['!cols'] = [
                    { width: 15 },
                    { width: 10 },
                    { width: 10 },
                    { width: 25 } // payeeId列需要更宽
                ];
                
                XLSX.utils.book_append_sheet(workbook, worksheet, 'TestData');

                // 生成文件名
                const fileName = `electron_test_${Date.now()}.xlsx`;
                
                // 使用Blob方式导出
                const workbookOutput = XLSX.write(workbook, {
                    bookType: 'xlsx',
                    type: 'array'
                });
                
                const blob = new Blob([workbookOutput], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                });
                
                const link = document.createElement('a');
                const url = URL.createObjectURL(blob);
                link.setAttribute('href', url);
                link.setAttribute('download', fileName);
                link.style.visibility = 'hidden';
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
                URL.revokeObjectURL(url);

                log(`✅ Excel导出成功！<br>文件名: ${fileName}<br>数据行数: ${data.length}<br>所有字段已保存为文本格式<br>请检查payeeId字段是否完整显示`, 'success');
                
            } catch (error) {
                log(`❌ 导出失败: ${error.message}<br>错误详情: ${error.stack}`, 'error');
                console.error('Export error:', error);
            }
        }

        // 页面加载时自动测试require
        window.onload = function() {
            testRequire();
        };
    </script>
</body>
</html> 